package com.cinc.ebc.fi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cinc.ebc.fi.constant.*;
import com.cinc.ebc.fi.entity.*;
import com.cinc.ebc.fi.mapper.*;
import com.cinc.ebc.fi.service.IFiOtherPaymentService;
import com.cinc.ebc.fi.service.IFiPaymentVerifyService;
import com.cinc.ebc.mm.constant.StockInOutDetailVerifyStatus;
import com.cinc.ebc.mm.entity.MmStockInOutDetail;
import com.cinc.ebc.mm.mapper.MmStockInOutDetailMapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 付款核销
 * @Author: jeecg-boot
 * @Date: 2022-12-28
 * @Version: V1.0
 */
@Service
public class FiPaymentVerifyServiceImpl extends ServiceImpl<FiPaymentVerifyMapper, FiPaymentVerify> implements IFiPaymentVerifyService {

    @Autowired
    private FiPaymentVerifyMapper fiPaymentVerifyMapper;
    @Autowired
    private FiPaymentVerifyDetailMapper fiPaymentVerifyDetailMapper;

    @Autowired
    private FiPaymentVoucherMapper fiPaymentVoucherMapper;

    @Autowired
    private FiApOriginalAmountMapper fiApOriginalAmountMapper;

    @Autowired
    private MmStockInOutDetailMapper mmStockInOutDetailMapper;

    @Autowired
    private FiOtherPaymentDetailMapper fiOtherPaymentDetailMapper;

    @Autowired
    private FiOtherPaymentMapper fiOtherPaymentMapper;

    @Override
    @Transactional
    public void saveMain(FiPaymentVerify fiPaymentVerify, List<FiPaymentVerifyDetail> fiPaymentVerifyDetailList) {
        if (oConvertUtils.isEmpty(fiPaymentVerify.getSourceId())) {
            throw new RuntimeException("付款单不能为空");
        }
        BigDecimal verifyAmount = fiPaymentVerify.getVerifyAmount() == null ? BigDecimal.ZERO : fiPaymentVerify.getVerifyAmount();
        if (verifyAmount.compareTo(BigDecimal.ZERO) != 1) {
            throw new RuntimeException("核销金额必须大于零");
        }
        // 付款单处理
        handleVerifyVoucher(fiPaymentVerify);
        // 保存主表
        fiPaymentVerifyMapper.insert(fiPaymentVerify);
        // 保存核销明细
        if (fiPaymentVerifyDetailList != null && fiPaymentVerifyDetailList.size() > 0) {
            for (FiPaymentVerifyDetail entity : fiPaymentVerifyDetailList) {
                //外键设置
                entity.setMainId(fiPaymentVerify.getId());
                BigDecimal detailVerifyAmount = entity.getVerifyAmount() == null ? BigDecimal.ZERO : entity.getVerifyAmount();
                if (detailVerifyAmount.compareTo(BigDecimal.ZERO) != 1) {
                    throw new RuntimeException("明细核销金额必须大于零");
                }
                // 业务单据处理
                handleVerifyDetail(entity);
                //
                fiPaymentVerifyDetailMapper.insert(entity);
            }
        }
    }

    /**
     * 处理付款单
     *
     * @param fiPaymentVerify
     */
    private void handleVerifyVoucher(FiPaymentVerify fiPaymentVerify) {
        FiPaymentVerify fiPaymentVerifyHis = fiPaymentVerifyMapper.selectById(fiPaymentVerify.getId());
        if (PaymentVerifyType.TYPE_03.equals(fiPaymentVerify.getVerifyType())) {
            // 期初预付款
            FiApOriginalAmount fiApOriginalAmount = fiApOriginalAmountMapper.selectById(fiPaymentVerify.getSourceId());
            if (AparInitialStatus.STATUS_10.equals(fiApOriginalAmount.getStatus())) {
                throw new RuntimeException("期初预付款未初始化，不能核销");
            }
            if (AparSettleStatus.STATUS_30.equals(fiApOriginalAmount.getSettleStatus())) {
                throw new RuntimeException("期初预付款已核销完成，不能核销");
            }
            BigDecimal originalVerifyAmount = fiApOriginalAmount.getVerifyAmount() == null ? BigDecimal.ZERO : fiApOriginalAmount.getVerifyAmount();
            BigDecimal verifyAmount = fiPaymentVerify.getVerifyAmount() == null ? BigDecimal.ZERO : fiPaymentVerify.getVerifyAmount();
            BigDecimal finalVerifyAmount = null;
            if (fiPaymentVerifyHis == null) {
                finalVerifyAmount = originalVerifyAmount.add(verifyAmount);
            } else {
                BigDecimal verifyAmountHis = fiPaymentVerifyHis.getVerifyAmount() == null ? BigDecimal.ZERO : fiPaymentVerifyHis.getVerifyAmount();
                finalVerifyAmount = originalVerifyAmount.subtract(verifyAmountHis).add(verifyAmount);
            }
            if (finalVerifyAmount.compareTo(fiApOriginalAmount.getInitialBalance().abs()) == 1) {
                throw new RuntimeException("期初预付款累计核销金额不能超出预付款余额");
            }
            FiApOriginalAmount originalAmountModify = new FiApOriginalAmount();
            originalAmountModify.setId(fiPaymentVerify.getSourceId());
            originalAmountModify.setVerifyAmount(finalVerifyAmount);
            fiApOriginalAmountMapper.updateById(originalAmountModify);
        } else {
            // 预付款、直接付款
            FiPaymentVoucher fiPaymentVoucher = fiPaymentVoucherMapper.selectById(fiPaymentVerify.getSourceId());
            if (AparVoucherStatus.STATUS_01.equals(fiPaymentVoucher.getStatus())) {
                throw new RuntimeException("付款单未审核，不能核销");
            }
            if (AparVoucherStatus.STATUS_03.equals(fiPaymentVoucher.getStatus())) {
                throw new RuntimeException("付款单已核销完成，不能核销");
            }
            BigDecimal voucherVerifyAmount = fiPaymentVoucher.getVerifyAmount() == null ? BigDecimal.ZERO : fiPaymentVoucher.getVerifyAmount();
            BigDecimal verifyAmount = fiPaymentVerify.getVerifyAmount() == null ? BigDecimal.ZERO : fiPaymentVerify.getVerifyAmount();
            BigDecimal finalVerifyAmount = null;
            if (fiPaymentVerifyHis == null) {
                finalVerifyAmount = voucherVerifyAmount.add(verifyAmount);
            } else {
                BigDecimal verifyAmountHis = fiPaymentVerifyHis.getVerifyAmount() == null ? BigDecimal.ZERO : fiPaymentVerifyHis.getVerifyAmount();
                finalVerifyAmount = voucherVerifyAmount.subtract(verifyAmountHis).add(verifyAmount);
            }
            if (finalVerifyAmount.compareTo(fiPaymentVoucher.getActAmount().abs()) == 1) {
                throw new RuntimeException("付款单累计核销金额不能超出实付金额");
            }
            FiPaymentVoucher fiPaymentVoucherModify = new FiPaymentVoucher();
            fiPaymentVoucherModify.setId(fiPaymentVoucher.getId());
            fiPaymentVoucherModify.setVerifyAmount(finalVerifyAmount);
            fiPaymentVoucherModify.setUnverifyAmount(fiPaymentVoucher.getActAmount().subtract(finalVerifyAmount));
            fiPaymentVoucherMapper.updateById(fiPaymentVoucherModify);
        }
    }

    /**
     * 处理业务单据
     *
     * @param entity
     */
    private void handleVerifyDetail(FiPaymentVerifyDetail entity) {
        FiPaymentVerifyDetail entityHis = fiPaymentVerifyDetailMapper.selectById(entity.getId());
        if (AparVoucherDetailBillType.AP_01.equals(entity.getBillType())) {
            //采购入库单
            MmStockInOutDetail stockInOutDetail = mmStockInOutDetailMapper.selectById(entity.getBillId());
            if (stockInOutDetail.getVerifyStatus().equals(StockInOutDetailVerifyStatus.STATUS_03)) {
                throw new RuntimeException("采购入库单已完成，不能再核销");
            }
            BigDecimal sumPrice = stockInOutDetail.getSumPrice() == null ? BigDecimal.ZERO : stockInOutDetail.getSumPrice();
            BigDecimal stockVerifyAmount = stockInOutDetail.getVerifyAmount() == null ? BigDecimal.ZERO : stockInOutDetail.getVerifyAmount();
            BigDecimal verifyAmount = entity.getVerifyAmount() == null ? BigDecimal.ZERO : entity.getVerifyAmount();
            BigDecimal finalAmount = BigDecimal.ZERO;
            if (entityHis == null) {
                finalAmount = verifyAmount.add(stockVerifyAmount);
            } else {
                BigDecimal verifyAmountHis = entityHis.getVerifyAmount() == null ? BigDecimal.ZERO : entityHis.getVerifyAmount();
                finalAmount = stockVerifyAmount.subtract(verifyAmountHis).add(verifyAmount);
            }
            if (finalAmount.compareTo(sumPrice) == 1) {
                throw new RuntimeException("核销金额不能大于单据金额");
            }
            MmStockInOutDetail stockInOutDetailModify = new MmStockInOutDetail();
            stockInOutDetailModify.setId(entity.getBillId());
            stockInOutDetailModify.setVerifyDate(new Date());
            stockInOutDetailModify.setVerifyAmount(finalAmount);
            mmStockInOutDetailMapper.updateById(stockInOutDetailModify);
        } else if(AparVoucherDetailBillType.AP_04.equals(entity.getBillType())) {
            // 应付期初余额
            FiApOriginalAmount fiApOriginalAmount = fiApOriginalAmountMapper.selectById(entity.getBillId());
            if (AparInitialStatus.STATUS_10.equals(fiApOriginalAmount.getStatus())) {
                throw new RuntimeException("期初应付未初始化，不能核销");
            }
            if (fiApOriginalAmount.getSettleStatus().equals(AparSettleStatus.STATUS_30)) {
                throw new RuntimeException("期初应付已结算完成，不能再核销");
            }
            BigDecimal originalVerifyAmount = fiApOriginalAmount.getVerifyAmount() == null ? BigDecimal.ZERO : fiApOriginalAmount.getVerifyAmount();
            BigDecimal initialBalance = fiApOriginalAmount.getInitialBalance() == null ? BigDecimal.ZERO : fiApOriginalAmount.getInitialBalance();
            if (originalVerifyAmount.compareTo(initialBalance) == 1) {
                throw new RuntimeException("期初应付核销金额异常");
            }
            BigDecimal verifyAmount = entity.getVerifyAmount() == null ? BigDecimal.ZERO : entity.getVerifyAmount();
            BigDecimal finalAmount = BigDecimal.ZERO;
            if (entityHis == null) {
                finalAmount = verifyAmount.add(originalVerifyAmount);
            } else {
                BigDecimal verifyAmountHis = entityHis.getVerifyAmount() == null ? BigDecimal.ZERO : entityHis.getVerifyAmount();
                finalAmount = originalVerifyAmount.subtract(verifyAmountHis).add(verifyAmount);
            }
            if (finalAmount.compareTo(initialBalance) == 1) {
                throw new RuntimeException("核销金额不能大于期初余额");
            }
            FiApOriginalAmount fiApOriginalAmountModify = new FiApOriginalAmount();
            fiApOriginalAmountModify.setId(entity.getBillId());
            fiApOriginalAmountModify.setVerifyAmount(finalAmount);
            fiApOriginalAmountMapper.updateById(fiApOriginalAmountModify);
        }else if (AparVoucherDetailBillType.AP_03.equals(entity.getBillType())) {
            // 其他应付单
            FiOtherPaymentDetail fiOtherPaymentDetail = fiOtherPaymentDetailMapper.selectById(entity.getBillId());
            if (fiOtherPaymentDetail.getStatus().equals(AparOtherDetailStatus.STATUS_03)) {
                throw new RuntimeException("其他应付单已完成，不能再核销");
            }
            BigDecimal otherPaymentAmount = fiOtherPaymentDetail.getPaymentAmount() == null ? BigDecimal.ZERO : fiOtherPaymentDetail.getPaymentAmount();
            BigDecimal otherVerifyAmount = fiOtherPaymentDetail.getVerifyAmount() == null ? BigDecimal.ZERO : fiOtherPaymentDetail.getVerifyAmount();
            BigDecimal verifyAmount = entity.getVerifyAmount() == null ? BigDecimal.ZERO : entity.getVerifyAmount();
            BigDecimal finalAmount = BigDecimal.ZERO;
            if (entityHis == null) {
                finalAmount = verifyAmount.add(otherVerifyAmount);
            } else {
                BigDecimal verifyAmountHis = entityHis.getVerifyAmount() == null ? BigDecimal.ZERO : entityHis.getVerifyAmount();
                finalAmount = otherVerifyAmount.subtract(verifyAmountHis).add(verifyAmount);
            }
            if (finalAmount.compareTo(otherPaymentAmount) == 1) {
                throw new RuntimeException("核销金额不能大于单据金额");
            }
            FiOtherPaymentDetail fiOtherPaymentDetailModify = new FiOtherPaymentDetail();
            fiOtherPaymentDetailModify.setId(entity.getBillId());
            fiOtherPaymentDetailModify.setVerifyAmount(finalAmount);
            fiOtherPaymentDetailMapper.updateById(fiOtherPaymentDetailModify);
            // 更新主表核销金额
            fiOtherPaymentMapper.autoUpdateVerifyAmount(fiOtherPaymentDetail.getMainId());
        } else {
            throw new RuntimeException("核销明细的单据类型异常，无法处理");
        }
    }

    @Override
    @Transactional
    public void updateMain(FiPaymentVerify fiPaymentVerify, List<FiPaymentVerifyDetail> fiPaymentVerifyDetailList) {
        FiPaymentVerify fiPaymentVerifyHis = fiPaymentVerifyMapper.selectById(fiPaymentVerify.getId());
        if ("1".equals(fiPaymentVerifyHis.getAuditFlag())) {
            throw new RuntimeException("已经审核，不能修改");
        }
        if (oConvertUtils.isEmpty(fiPaymentVerify.getSourceId())) {
            throw new RuntimeException("付款单不能为空");
        }
        BigDecimal verifyAmount = fiPaymentVerify.getVerifyAmount() == null ? BigDecimal.ZERO : fiPaymentVerify.getVerifyAmount();
        if (verifyAmount.compareTo(BigDecimal.ZERO) != 1) {
            throw new RuntimeException("核销金额必须大于零");
        }
        // 付款单处理
        handleVerifyVoucher(fiPaymentVerify);
        // 保存主表
        fiPaymentVerifyMapper.updateById(fiPaymentVerify);

        // 获取子表数据
        LambdaQueryWrapper<FiPaymentVerifyDetail> detailQuery = new LambdaQueryWrapper<>();
        detailQuery.eq(FiPaymentVerifyDetail::getMainId, fiPaymentVerify.getId());
        List<FiPaymentVerifyDetail> paymentVerifyDetailHisList = fiPaymentVerifyDetailMapper.selectList(detailQuery);
        List<String> detailIdList = paymentVerifyDetailHisList.stream().map(FiPaymentVerifyDetail::getId).collect(Collectors.toList());
        List<String> delIdList = detailIdList;
        // 子表数据更新
        if (oConvertUtils.listIsNotEmpty(fiPaymentVerifyDetailList)) {
            for (FiPaymentVerifyDetail entity : fiPaymentVerifyDetailList) {
                BigDecimal detailVerifyAmount = entity.getVerifyAmount() == null ? BigDecimal.ZERO : entity.getVerifyAmount();
                if (detailVerifyAmount.compareTo(BigDecimal.ZERO) != 1) {
                    throw new RuntimeException("核销金额必须大于零");
                }
                entity.setMainId(fiPaymentVerify.getId());
                //业务单据处理
                handleVerifyDetail(entity);
                //
                if (!detailIdList.contains(entity.getId())) {
                    //新增
                    fiPaymentVerifyDetailMapper.insert(entity);
                } else {
                    //修改
                    fiPaymentVerifyDetailMapper.updateById(entity);
                }
            }
            //获取删除记录ID
            List<String> curIdList = fiPaymentVerifyDetailList.stream().map(FiPaymentVerifyDetail::getId).collect(Collectors.toList());
            delIdList = detailIdList.stream().filter(id -> !curIdList.contains(id)).collect(Collectors.toList());
        }
        // 删除记录处理
        if (oConvertUtils.listIsNotEmpty(delIdList)) {
            for (String id : delIdList) {
                // 业务单据处理
                FiPaymentVerifyDetail entityHis = fiPaymentVerifyDetailMapper.selectById(id);
                FiPaymentVerifyDetail entityDel = new FiPaymentVerifyDetail();
                entityDel.setId(id);
                entityDel.setBillId(entityHis.getBillId());
                entityDel.setBillType(entityHis.getBillType());
                entityDel.setVerifyAmount(BigDecimal.ZERO);
                handleVerifyDetail(entityDel);
                // 删除
                fiPaymentVerifyDetailMapper.deleteById(id);
            }
        }
    }

    @Override
    @Transactional
    public void delMain(String id) {
        FiPaymentVerify fiPaymentVerify = fiPaymentVerifyMapper.selectById(id);
        if ("1".equals(fiPaymentVerify.getAuditFlag())) {
            throw new RuntimeException("已经审核，不能删除");
        }
        List<FiPaymentVerifyDetail> fiPaymentVerifyDetailList = fiPaymentVerifyDetailMapper.selectByMainId(id);
        if (oConvertUtils.listIsNotEmpty(fiPaymentVerifyDetailList)) {
            for (FiPaymentVerifyDetail fiPaymentVerifyDetail : fiPaymentVerifyDetailList) {
                //处理业务单据
                FiPaymentVerifyDetail detailDel = new FiPaymentVerifyDetail();
                detailDel.setId(fiPaymentVerifyDetail.getId());
                detailDel.setBillId(fiPaymentVerifyDetail.getBillId());
                detailDel.setBillType(fiPaymentVerifyDetail.getBillType());
                detailDel.setVerifyAmount(BigDecimal.ZERO);
                handleVerifyDetail(detailDel);
            }
        }
        //
        fiPaymentVerifyDetailMapper.deleteByMainId(id);
        fiPaymentVerifyMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for (Serializable id : idList) {
            delMain(id.toString());
        }
    }

    @Transactional
    @Override
    public void audit(List<FiPaymentVerify> fiPaymentVerifyList) {
        for (FiPaymentVerify fiPaymentVerify : fiPaymentVerifyList) {
            FiPaymentVerify fiPaymentVerifyHis = fiPaymentVerifyMapper.selectById(fiPaymentVerify.getId());
            if ("1".equals(fiPaymentVerifyHis.getAuditFlag())) {
                throw new RuntimeException("已经审核，不能修改");
            }
            if (oConvertUtils.isEmpty(fiPaymentVerifyHis.getSourceId())) {
                throw new RuntimeException("付款单不能为空");
            }
            BigDecimal verifyAmount = fiPaymentVerifyHis.getVerifyAmount() == null ? BigDecimal.ZERO : fiPaymentVerifyHis.getVerifyAmount();
            if (verifyAmount.compareTo(BigDecimal.ZERO) != 1) {
                throw new RuntimeException("核销金额必须大于零");
            }
            FiPaymentVerify fiPaymentVerifyModify = new FiPaymentVerify();
            fiPaymentVerifyModify.setId(fiPaymentVerify.getId());
            fiPaymentVerifyModify.setAuditDate(new Date());
            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            fiPaymentVerifyModify.setAuditor(sysUser != null ? sysUser.getUsername() : null);
            fiPaymentVerifyModify.setAuditFlag("1");
            fiPaymentVerifyMapper.updateById(fiPaymentVerifyModify);
            // 付款单状态处理
            handleVerifyVoucherStatus(fiPaymentVerify);
            // 业务单据状态处理
            List<FiPaymentVerifyDetail> fiPaymentVerifyDetailList = fiPaymentVerifyDetailMapper.selectByMainId(fiPaymentVerify.getId());
            for (FiPaymentVerifyDetail fiPaymentVerifyDetail : fiPaymentVerifyDetailList) {
                handleVerifyDetailStatus(fiPaymentVerifyDetail);
            }
        }
    }

    /**
     * 付款单状态处理
     *
     * @param fiPaymentVerify
     */
    private void handleVerifyVoucherStatus(FiPaymentVerify fiPaymentVerify) {
        if (PaymentVerifyType.TYPE_03.equals(fiPaymentVerify.getVerifyType())) {
            // 期初预付款
            FiApOriginalAmount fiApOriginalAmount = fiApOriginalAmountMapper.selectById(fiPaymentVerify.getSourceId());
            if (AparInitialStatus.STATUS_10.equals(fiApOriginalAmount.getStatus())) {
                throw new RuntimeException("期初预付款未初始化");
            }
            BigDecimal originalVerifyAmount = fiApOriginalAmount.getVerifyAmount() == null ? BigDecimal.ZERO : fiApOriginalAmount.getVerifyAmount();
            BigDecimal initialBlance = fiApOriginalAmount.getInitialBalance() == null ? BigDecimal.ZERO : fiApOriginalAmount.getInitialBalance().abs();
            if (originalVerifyAmount.compareTo(initialBlance) == 1) {
                throw new RuntimeException("期初预付款核销金额异常");
            }
            FiApOriginalAmount originalAmountModify = new FiApOriginalAmount();
            originalAmountModify.setId(fiPaymentVerify.getSourceId());
            if (originalVerifyAmount.compareTo(initialBlance) == 0) {
                originalAmountModify.setSettleStatus(AparSettleStatus.STATUS_30);
            } else if (originalVerifyAmount.compareTo(BigDecimal.ZERO) == 1) {
                originalAmountModify.setSettleStatus(AparSettleStatus.STATUS_20);
            } else {
                originalAmountModify.setSettleStatus(AparSettleStatus.STATUS_10);
            }
            fiApOriginalAmountMapper.updateById(originalAmountModify);
        } else {
            // 预付款、直接付款
            FiPaymentVoucher fiPaymentVoucher = fiPaymentVoucherMapper.selectById(fiPaymentVerify.getSourceId());
            if (AparVoucherStatus.STATUS_01.equals(fiPaymentVoucher.getStatus())) {
                throw new RuntimeException("付款单未审核");
            }
            BigDecimal voucherVerifyAmount = fiPaymentVoucher.getVerifyAmount() == null ? BigDecimal.ZERO : fiPaymentVoucher.getVerifyAmount();
            BigDecimal actAmount = fiPaymentVoucher.getActAmount() == null ? BigDecimal.ZERO : fiPaymentVoucher.getActAmount();
            if (voucherVerifyAmount.compareTo(actAmount) == 1) {
                throw new RuntimeException("付款单核销金额异常");
            }
            FiPaymentVoucher fiPaymentVoucherModify = new FiPaymentVoucher();
            fiPaymentVoucherModify.setId(fiPaymentVoucher.getId());
            if (voucherVerifyAmount.compareTo(actAmount) == 0) {
                fiPaymentVoucherModify.setStatus(AparVoucherStatus.STATUS_03);
            } else if (voucherVerifyAmount.compareTo(BigDecimal.ZERO) == 1) {
                fiPaymentVoucherModify.setStatus(AparVoucherStatus.STATUS_02);
            } else {
                fiPaymentVoucherModify.setStatus(AparVoucherStatus.STATUS_01);
            }
            fiPaymentVoucherMapper.updateById(fiPaymentVoucherModify);
        }
    }

    /**
     * 业务单据状态处理
     *
     * @param entity
     */
    private void handleVerifyDetailStatus(FiPaymentVerifyDetail entity) {
        if (AparVoucherDetailBillType.AP_01.equals(entity.getBillType())) {
            //采购入库单
            MmStockInOutDetail stockInOutDetail = mmStockInOutDetailMapper.selectById(entity.getBillId());
            BigDecimal sumPrice = stockInOutDetail.getSumPrice() == null ? BigDecimal.ZERO : stockInOutDetail.getSumPrice();
            BigDecimal stockVerifyAmount = stockInOutDetail.getVerifyAmount() == null ? BigDecimal.ZERO : stockInOutDetail.getVerifyAmount();
            if (stockVerifyAmount.compareTo(sumPrice) == 1) {
                throw new RuntimeException("采购入库单核销金额异常");
            }
            MmStockInOutDetail stockInOutDetailModify = new MmStockInOutDetail();
            stockInOutDetailModify.setId(stockInOutDetail.getId());
            if (stockVerifyAmount.compareTo(sumPrice) == 0) {
                stockInOutDetailModify.setVerifyStatus(StockInOutDetailVerifyStatus.STATUS_03);
            } else if (stockVerifyAmount.compareTo(BigDecimal.ZERO) == 1) {
                stockInOutDetailModify.setVerifyStatus(StockInOutDetailVerifyStatus.STATUS_02);
            } else {
                stockInOutDetailModify.setVerifyStatus(StockInOutDetailVerifyStatus.STATUS_01);
            }
            mmStockInOutDetailMapper.updateById(stockInOutDetailModify);
        } else if (AparVoucherDetailBillType.AP_04.equals(entity.getBillType())) {
            // 应付期初余额
            FiApOriginalAmount fiApOriginalAmount = fiApOriginalAmountMapper.selectById(entity.getBillId());
            BigDecimal initialBalance = fiApOriginalAmount.getInitialBalance() == null ? BigDecimal.ZERO : fiApOriginalAmount.getInitialBalance();
            BigDecimal originalVerifyAmount = fiApOriginalAmount.getVerifyAmount() == null ? BigDecimal.ZERO : fiApOriginalAmount.getVerifyAmount();
            if (originalVerifyAmount.compareTo(initialBalance) == 1) {
                throw new RuntimeException("期初应付核销金额异常");
            }
            FiApOriginalAmount fiApOriginalAmountModify = new FiApOriginalAmount();
            fiApOriginalAmountModify.setId(fiApOriginalAmount.getId());
            if (originalVerifyAmount.compareTo(initialBalance) == 0) {
                fiApOriginalAmountModify.setSettleStatus(AparSettleStatus.STATUS_30);
            } else if (originalVerifyAmount.compareTo(BigDecimal.ZERO) == 1) {
                fiApOriginalAmountModify.setSettleStatus(AparSettleStatus.STATUS_20);
            } else {
                fiApOriginalAmountModify.setSettleStatus(AparSettleStatus.STATUS_10);
            }
            fiApOriginalAmountMapper.updateById(fiApOriginalAmountModify);
        } else if (AparVoucherDetailBillType.AP_03.equals(entity.getBillType())) {
            // 其他应付单
            FiOtherPaymentDetail fiOtherPaymentDetail = fiOtherPaymentDetailMapper.selectById(entity.getBillId());
            BigDecimal otherPaymentAmount = fiOtherPaymentDetail.getPaymentAmount() == null ? BigDecimal.ZERO : fiOtherPaymentDetail.getPaymentAmount();
            BigDecimal otherVerifyAmount = fiOtherPaymentDetail.getVerifyAmount() == null ? BigDecimal.ZERO : fiOtherPaymentDetail.getVerifyAmount();
            if (otherVerifyAmount.compareTo(otherPaymentAmount) == 1) {
                throw new RuntimeException("其他应付单核销金额异常");
            }
            FiOtherPaymentDetail fiOtherPaymentDetailModify = new FiOtherPaymentDetail();
            fiOtherPaymentDetailModify.setId(fiOtherPaymentDetail.getId());
            if (otherVerifyAmount.compareTo(otherPaymentAmount) == 0) {
                fiOtherPaymentDetailModify.setStatus(AparOtherDetailStatus.STATUS_03);
            } else if (otherVerifyAmount.compareTo(BigDecimal.ZERO) == 1) {
                fiOtherPaymentDetailModify.setStatus(AparOtherDetailStatus.STATUS_02);
            } else {
                fiOtherPaymentDetailModify.setStatus(AparOtherDetailStatus.STATUS_01);
            }
            fiOtherPaymentDetailMapper.updateById(fiOtherPaymentDetailModify);
            // 更新主表状态
            fiOtherPaymentMapper.autoUpdateStatus(fiOtherPaymentDetail.getMainId());
        }
    }
}
